<?php
/**
 * Created by PhpStorm.
 * User: FDG296
 * Date: 2017/7/10
 * Time: 15:33
 */
namespace common\models;

use Yii;
use yii\db\ActiveRecord;

class GoodsSpec extends ActiveRecord{

	const USE_FLG_YES	= 1;
	const USE_FLG_NO	= 0;

    public static function tableName(){
        return '{{%goods_spec}}';
    }

    //获取商品颜色规格
    public function getColorSpecImg($spec_id){
        $spec = array();
        $where = ['use_flg'=>1,'id'=>$spec_id];
        $result = $this::find()->where($where)->orderBy('sort asc')->select('id,img_url')->asArray()->all();
        if(!empty($result)){
            foreach($result as $value){
                $spec[$value['id']]['id'] = $value['id'];
                $spec[$value['id']]['img_url'] = $value['img_url'];
            }
        }
        return $spec;
    }

	/**
	 * @param $mainGoodsId 主商品id
	 * @return array|\yii\db\ActiveRecord[]
	 */
	public static function getSpec($mainGoodsId){
		//获取商品的信息
		$goodSale		= array_column(GoodsSale::getMainGoodList($mainGoodsId),'spec');

		//返回数据
		return self::find()
			->where(['use_flg'=>self::USE_FLG_YES])
			->andWhere(['in','id',$goodSale])
			->asArray()
			->all();
	}


	/**
	 * @param $mainGoodsId 主商品id
	 * @return array|\yii\db\ActiveRecord[]
	 */
	public static function getSpecColor($mainGoodsId){

		//获取商品的信息
		$goodSale		= array_column(GoodsSale::getMainGoodList($mainGoodsId),'spec_color');

		//返回数据
		return self::find()
			->where([
				self::tableName().'.use_flg'=>self::USE_FLG_YES
			])
			->joinWith('specRelation')
			->andWhere(['in',self::tableName().'.id',$goodSale])
			->asArray()
			->all();
	}

	/**
	 * @param 关联表信息
	 * @return \yii\db\ActiveQuery
	 */
	public function getSpecRelation(){
		return $this->hasOne(GoodsSpecRelation::className(),['spec_id'=>'id']);
	}


}